package splab.ufcg.edu.br.trace.facade;

import java.io.File;
import java.io.Serializable;

import splab.ufcg.edu.br.trace.entities.*;
import splab.ufcg.edu.br.trace.exceptions.*;
import splab.ufcg.edu.br.trace.indexer.*;
import splab.ufcg.edu.br.trace.interfaces.*;
import splab.ufcg.edu.br.trace.querier.ApacheSolrJQuerier;
import splab.ufcg.edu.br.trace.query.entities.QueryParameter;
import splab.ufcg.edu.br.trace.search.util.IndexerProperties;
import splab.ufcg.edu.br.trace.translator.TestlinkTranslator;
import splab.ufcg.edu.br.trace.writer.TraceLinkWriter;



public class TraceabilityFacade implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String WRITER_TESTLINK_OUTPUT = "/Users/Arthur/Dropbox/Mestrado-ArthurMarques-Shared/Traceability-Tool/traceability-tool/src/main/resources/writer/testlink.xml";

	private static final String WRITER_DIRECTORY = "/Users/Arthur/Dropbox/Mestrado-ArthurMarques-Shared/Traceability-Tool/traceability-tool/src/main/resources/writer";
	
	private final String TESTLINK_CONNECTION = "/Users/Arthur/Dropbox/Mestrado-ArthurMarques-Shared/Traceability-Tool/traceability-tool/src/main/resources/parser-testlink.properties";
	
	private final String APACHE_INDEXER = "/Users/Arthur/Dropbox/Mestrado-ArthurMarques-Shared/Traceability-Tool/traceability-tool/src/main/resources/indexer.properties";

	private final String APACHE_CONNECTION = "/Users/Arthur/Dropbox/Mestrado-ArthurMarques-Shared/Traceability-Tool/traceability-tool/src/main/resources/apache-solr.properties";
	
	private Translatable writer;

	private Searchable search;

	private Indexable indexer;

	private Traceable testLink;

	private TraceLinkList tracelinkList;

	private static TraceabilityFacade instance;

	public TraceabilityFacade() {
		try {
		
		splab.ufcg.edu.br.trace.testlink.util.ConnectionProperties testLinkProperties = new splab.ufcg.edu.br.trace.testlink.util.ConnectionProperties(TESTLINK_CONNECTION);
		splab.ufcg.edu.br.trace.search.util.ConnectionProperties apacheConnection = new splab.ufcg.edu.br.trace.search.util.ConnectionProperties(APACHE_CONNECTION);
		IndexerProperties indexerProperties = new IndexerProperties(APACHE_INDEXER);
		
		this.writer = new TraceLinkWriter();
		this.search = new ApacheSolrJQuerier(apacheConnection);
		this.indexer = new ApacheSolrJIndexer(indexerProperties, apacheConnection);
		this.testLink = new TestlinkTranslator(testLinkProperties);

		
		
		} catch (PropertiesException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static TraceabilityFacade getInstance() {
		if (instance == null) {
			instance = new TraceabilityFacade();
		}
		return instance;
	}

	public boolean indexTraceLinks() {

		try {
			File directory = new File(WRITER_DIRECTORY);
			this.indexer.indexTraceLinks(directory);
		} catch (IndexException ex) {
			ex.printStackTrace();

			return false;
		}

		return true;
	}

	public boolean extractTraceLinks() {
		try {

			TraceLinkList traceLinks = this.testLink.getTraceLinks();
			
			File file = new File(WRITER_TESTLINK_OUTPUT);
			this.writer.write(file, traceLinks);

		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}
	
	public TraceLinkList readTraceLinks(){
		
		if (this.tracelinkList == null){
			this.tracelinkList = new TraceLinkList();
		}
		
		try {
			
			File directory = new File(WRITER_DIRECTORY);
			
			if (directory.exists()) {

				File[] fileList = directory.listFiles();
					for (File file : fileList) {
						TraceLinkList readLinks = this.writer.read(file);
						this.tracelinkList.addAll(readLinks.getTraceLinks());					
				}

			}

			return tracelinkList;

		} catch (Exception ex) {
			ex.printStackTrace();
			
		}
		return null;
	}

	public TraceLinkList queryTraceLinks(QueryParameter... parameters) {

		TraceLinkList tracelinks = null;

		try {
			tracelinks = this.search.queryTraceLins(parameters);
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return tracelinks;
	}
	
	public TraceLinkList getTraceLinkList(){
		return this.tracelinkList;
	}

}
